package com.example.testdemo;

import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.ui.activity.LayoutGameActivity;

import android.graphics.Point;
import android.util.Log;
import android.view.KeyEvent;

import com.example.testdemo.manager.CamerasManager;
import com.example.testdemo.manager.ResourcesManager;
import com.example.testdemo.manager.ScenesManager;
import com.example.testdemo.pools.PathPools;
import com.example.testdemo.scenes.BaseScene;

public class TankGameActivity extends LayoutGameActivity {    
	private final String TAG = TankGameActivity.class.getSimpleName();

	public static final int FPS = 30;

	private ScenesManager mScenesManager = ScenesManager.getInstance();
	private ResourcesManager mResourcesManager = ResourcesManager.getInstance();
	private CamerasManager mCameraManager = CamerasManager.getInstance();

	@Override
	protected int getLayoutID() {
		return R.layout.activity_game;
	}

	@Override
	protected int getRenderSurfaceViewID() {
		return R.id.gameSurfaceView;
	}

	@Override
	public Engine onCreateEngine(EngineOptions pEngineOptions) {
		return new LimitedFPSEngine(pEngineOptions, FPS) {
			@Override
			public void setScene(Scene pScene) {
				super.setScene(pScene);
				mScenesManager.setCurrentScene(pScene);
			}
		};
	}

	@Override
	public EngineOptions onCreateEngineOptions() {
		mCameraManager.initCamera(CamerasManager.CAMERA_ZOOM);
		EngineOptions options = new EngineOptions(true,
		        CamerasManager.ORIENTATION, new FillResolutionPolicy(),
		        mCameraManager.getCamera(CamerasManager.CAMERA_ZOOM));
		options.getTouchOptions().setNeedsMultiTouch(true);
		options.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
		options.setWakeLockOptions(WakeLockOptions.SCREEN_ON);

		Point size = new Point();
		getWindowManager().getDefaultDisplay().getSize(size);
		Log.d(TAG, "screen resolution : (" + size.x + "," + size.y + ")");
		return options;
	}

	@Override
	public void onCreateResources(
	        OnCreateResourcesCallback pOnCreateResourcesCallback)
	        throws Exception {
		mResourcesManager.loadResources(this, ScenesManager.SCENE_SPLASH);
		pOnCreateResourcesCallback.onCreateResourcesFinished();
	}

	@Override
	public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
	        throws Exception {
		mScenesManager.loadScene(this, ScenesManager.SCENE_SPLASH);
		pOnCreateSceneCallback.onCreateSceneFinished(mScenesManager
		        .getScene(ScenesManager.SCENE_SPLASH));
	}

	@Override
	public void onPopulateScene(Scene pScene,
	        OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
		mScenesManager.getScene(ScenesManager.SCENE_SPLASH).populateScene();
		getEngine().registerUpdateHandler(
		        new TimerHandler(2, new ITimerCallback() {
			        @Override
			        public void onTimePassed(TimerHandler pTimerHandler) {
				        getEngine().unregisterUpdateHandler(pTimerHandler);

				        // load game scene
				        mResourcesManager.loadResources(TankGameActivity.this,
				                ResourcesManager.RESOURCES_STAGE_1);
				        mResourcesManager.loadResources(TankGameActivity.this,
				                ResourcesManager.RESOURCES_CHARACTER);
				        mResourcesManager.loadResources(TankGameActivity.this,
				                ResourcesManager.RESOURCES_ITEMS);
				        mScenesManager.loadScene(TankGameActivity.this,
				                ScenesManager.SCENE_STAGE_1);
				        mScenesManager.getScene(ScenesManager.SCENE_STAGE_1)
				                .populateScene();

				        // unload splash scene
				        mScenesManager.unloadScene(ScenesManager.SCENE_SPLASH);
				        mResourcesManager
				                .unloadResources(ResourcesManager.RESOURCES_SPLASH);

				        // show game scene
				        getEngine().setScene(
				                mScenesManager
				                        .getScene(ScenesManager.SCENE_STAGE_1));
			        }
		        }));
		pOnPopulateSceneCallback.onPopulateSceneFinished();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		BaseScene scene = mScenesManager.getScene(mScenesManager
		        .getCurrentSceneIndex());
		boolean handled = scene.onKeyDown(keyCode, event);

		if (!handled) {
			return super.onKeyDown(keyCode, event);
		} else {
			return true;
		}
	}

	@Override
	protected void onDestroy() {
		PathPools.getInstances().free();
		CamerasManager.getInstance().free();
		ScenesManager.getInstance().free();
		ResourcesManager.getInstance().free();
		super.onDestroy();
	}
}
